package com.jcfk.eam.service.base;


import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.dao.base.EamPersonPowerDao;
import com.jcfk.eam.domain.dto.base.EamPersonAssetPowerDTO;
import com.jcfk.eam.domain.dto.base.EamPersonLocationPowerDTO;
import com.jcfk.eam.domain.dto.base.EamPersonPowerDTO;
import com.jcfk.eam.domain.po.base.EamPersonAssetPower;
import com.jcfk.eam.domain.po.base.EamPersonLocationPower;
import com.jcfk.eam.domain.po.base.EamPersonPower;
import com.jcfk.eam.domain.query.base.EamPersonPowerQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.jcfk.common.core.utils.IdUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 人员管理权限
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-08-20
 */
@Service
public class EamPersonPowerService extends BaseService<EamPersonPowerDao, EamPersonPower> {

    @Autowired
    private EamPersonLocationPowerService locationPowerService;

    @Autowired
    private EamPersonAssetPowerService assetPowerService;

    /**
     * EamPersonPower分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamPersonPowerDTO> queryByPage(DataPage<EamPersonPowerDTO> page, EamPersonPowerQuery query) {
        // 分页查询
        query.setUserName(StringUtils.isEmpty(query.getUserName()) ? query.getUserName() : query.getUserName() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamPersonPower是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamPersonPowerDTO dto) {
        QueryWrapper<EamPersonPower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        return RetResult.ok();
    }

    /**
     * 插入人员管理权限
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamPersonPowerDTO dto) {

        // 保存
        EamPersonPower item = new EamPersonPower();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);

        // 保存权限
        for (EamPersonAssetPowerDTO assetPower : dto.getAssetPowerList()) {
            assetPower.setPersonPowerId(item.getId());
        }
        for (EamPersonLocationPowerDTO locationPower : dto.getLocationPowerList()) {
            locationPower.setPersonPowerId(item.getId());
        }
        locationPowerService.saveList(item.getId(), dto.getLocationPowerList());
        assetPowerService.saveList(item.getId(), dto.getAssetPowerList());
    }

    /**
     * 更新人员管理权限
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamPersonPowerDTO dto) {

        // 保存
        EamPersonPower item = new EamPersonPower();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);

        // 保存权限
        for (EamPersonAssetPowerDTO assetPower : dto.getAssetPowerList()) {
            assetPower.setPersonPowerId(item.getId());
        }
        for (EamPersonLocationPowerDTO locationPower : dto.getLocationPowerList()) {
            locationPower.setPersonPowerId(item.getId());
        }
        locationPowerService.saveList(item.getId(), dto.getLocationPowerList());
        assetPowerService.saveList(item.getId(), dto.getAssetPowerList());
    }

    /**
     * 根据批量主键删除人员管理权限(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamPersonPowerDTO dto) {

        EamPersonPower item = new EamPersonPower();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamPersonPower> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamPersonPower item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取人员管理权限信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamPersonPowerDTO get(String id) {
        // 查询数据
        EamPersonPower item = this.baseMapper.selectById(id);
        EamPersonPowerDTO dto = new EamPersonPowerDTO();


        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        QueryWrapper<EamPersonLocationPower> qw = new QueryWrapper<>();
        qw.lambda().eq(EamPersonLocationPower::getPersonPowerId, id);
        List<EamPersonLocationPower> personLocationPowers = locationPowerService.list(qw);

        dto.setLocationPowerList(convertList(personLocationPowers, EamPersonLocationPowerDTO.class));
        QueryWrapper<EamPersonAssetPower> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamPersonAssetPower::getPersonPowerId, id);

        List<EamPersonAssetPower> eamPersonAssetPowers = assetPowerService.list(queryWrapper);
        dto.setAssetPowerList(convertList(eamPersonAssetPowers, EamPersonAssetPowerDTO.class));

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamPersonPowerDTO>
     */
    public List<EamPersonPowerDTO> getList(EamPersonPowerQuery query) {
        // 查询条件
        QueryWrapper<EamPersonPower> qw = new QueryWrapper<>();
        qw.lambda().eq(query.getAssetKind() != null, EamPersonPower::getAssetKind, query.getAssetKind());
        qw.lambda().eq(!StrUtil.isEmpty(query.getUserId()), EamPersonPower::getUserId, query.getUserId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getUserEmpNo()), EamPersonPower::getUserEmpNo, query.getUserEmpNo());
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getUserName()), EamPersonPower::getUserName, query.getUserName());
        qw.lambda().eq(!StrUtil.isEmpty(query.getDepartmentId()), EamPersonPower::getDepartmentId, query.getDepartmentId());
        // 查询数据
        List<EamPersonPower> list = super.list(qw);

        return convertList(list, EamPersonPowerDTO.class);
    }

    /**
     * 根据主键获取人员管理权限信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        EamPersonPower item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

